<!--
 * @Author: zl
 * @Date: 2021-02-09 15:24:23
 * @LastEditTime: 2021-11-30 18:35:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\views\layoutpages\system\components\usersEdit.vue
-->
<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- <span>{{ rowData }}</span> -->
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            label-width="80px"
            :inline="false"
        >
            <el-form-item label="字段名称" prop="label">
                <el-input
                    v-model="label"
                    placeholder="字段名称"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="字段类型" prop="type">
                <el-select
                    style="width: 100%"
                    v-model="type"
                    placeholder="表单类型"
                    clearable
                >
                    <el-option
                        v-for="item in formList"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                        :disabled="item.status == 0"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否必填">
                <el-radio-group v-model="verify">
                    <el-radio-button :label="1">是</el-radio-button>
                    <el-radio-button :label="0">否</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item
                label="选择值"
                v-if="type == 'select' || type == 'checkbox' || type == 'radio'"
                prop="list"
            >
                <el-input
                    v-model="list"
                    placeholder="默认值"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="上传张数" v-if="type == 'image'">
                <el-input-number
                    v-model="value"
                    :min="1"
                    :max="2"
                    step-strictly
                ></el-input-number>
            </el-form-item>
            <el-form-item
                label="默认值"
                v-if="type != 'image' && type != 'date'"
                prop="value"
            >
                <el-input
                    v-model="value"
                    placeholder="默认值"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input-number
                    v-model="sort"
                    :min="0"
                    step-strictly
                    :step="1"
                ></el-input-number>
            </el-form-item>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import {
    reactive,
    toRefs,
    ref,
    onMounted,
    defineProps,
    defineEmits,
} from "vue";
const rules = {
    label: [
        {
            required: true,
            message: "请输入字段名称",
            trigger: "blur",
        },
    ],
    type: [
        {
            required: true,
            message: "请选择字段类型",
            trigger: "blur",
        },
    ],
    list: [
        {
            required: true,
            message: "请输入选择值，以','隔开。案例（男,女）",
            trigger: "blur",
        },
    ],
};
const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};
const formRef = ref(null);
const form = reactive({
    label: "",
    type: "",
    verify: 1,
    value: "",
    list: "",
    sort: 0,
    formCode: localStorage.getItem("formCode"),
});
const { label, type, verify, value, list, sort } = toRefs(form);
const formList = ref([]);

/**
 * @description: 初始化
 * @param {*}
 * @return {*}
 */
rowData.value &&
    ((label.value = rowData.value.label),
    (type.value = rowData.value.type),
    (verify.value = rowData.value.verify),
    (list.value = rowData.value.list),
    (sort.value = rowData.value.sort),
    (value.value = rowData.value.value));
/**
 * @description: 获取字段类型
 * @param {*}
 * @return {*}
 */
const getFormList = async () => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: "zdlx",
    });
    if (code == "00") {
        formList.value = data;
    }
    formList.value = data;
};
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value == "添加") {
                res = await VE_API.system.formFiledAdd(form);
            } else {
                res = await VE_API.system.formFiledEdit({
                    id: rowData.value.id,
                    ...form,
                });
            }
            const { code } = res;
            if (code == "00") {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};
onMounted(async () => {
    await getFormList();
    // maxHeight(pagination, queryForm, toolBar, ve_max_height);
});
</script>

<style lang="scss" scoped></style>
